//
//  NRWIndexBarTableView.m
//  NRWMall
//
//  Created by 邢毅 on 2018/5/24.
//  Copyright © 2018年 com.anpxd. All rights reserved.
//

#import "NRWIndexBarView.h"

#import<AudioToolbox/AudioToolbox.h>

@interface NRWIndexBarViewCell : UITableViewCell
@property (copy, nonatomic) NSString *text;
@end

@implementation NRWIndexBarViewCell
{
    UILabel *label;
}
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        self.backgroundColor = self.contentView.backgroundColor = [UIColor clearColor];
        label = DPLabel().setTextColor(getColor(@"#323232")).setFont(getThemeFont(10)).setTextAlignment(NSTextAlignmentCenter);
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        self.addSubview(label);
    }
    return self;
}
-(void)layoutSubviews{
    [super layoutSubviews];
    label.frame = self.bounds;
}
- (void)setText:(NSString *)text {
    _text = text;
    label.text = text;
}
@end

@interface NRWIndexBarView()<UITableViewDelegate, UITableViewDataSource>
@end

CGFloat cellHeight = 16;
CGFloat cellWidth = 20;

@implementation NRWIndexBarView
{
    UITableView *tableView;
    NSInteger currentIndex;
}

- (instancetype)init
{
    self = [super init];
    if (self) {
        currentIndex = -1;
        self.backgroundColor = [UIColor clearColor];
        tableView = [[UITableView alloc] init];
        tableView.backgroundColor = [UIColor clearColor];
        tableView.delegate = self;
        tableView.dataSource = self;
        tableView.scrollEnabled = NO;
        tableView.userInteractionEnabled = NO;
        tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        tableView.showsVerticalScrollIndicator = NO;
        tableView.showsHorizontalScrollIndicator = NO;
        [tableView registerClass:[NRWIndexBarViewCell class] forCellReuseIdentifier:@"cell"];
        self.addSubview(tableView);
    }
    return self;
}

- (void)setIndexTitleArr:(NSArray<NSString *> *)indexTitleArr {
    
    _indexTitleArr = indexTitleArr;
    
    [tableView reloadData];
    
    [self setNeedsLayout];
}

- (void)layoutSubviews {
    
    [super layoutSubviews];
    
    self.frame = CGRectMake(SCREEN_WIDTH() - cellWidth, (self.superview.viewHeight - self.indexTitleArr.count * cellHeight) / 2, cellWidth, self.indexTitleArr.count * cellHeight);
    tableView.frame = self.bounds;
}

- (NSInteger)numberOfRowsInSection:(NSInteger)section {
    
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    return self.indexTitleArr.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    NRWIndexBarViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
    
    cell.text = self.indexTitleArr[indexPath.row];
    
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    return cellHeight;
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    
    if ([self.delegate respondsToSelector:@selector(indexBarViewDidStartTouch:)]) {
        
        [self.delegate indexBarViewDidStartTouch:self];
    }
    
    NSInteger index = [self getIndexFromTouches:touches];
    
    [self makeHock];
    
    if ([self.delegate respondsToSelector:@selector(indexBarView:didSelectAtIndex:title:)]) {
        
        [self.delegate indexBarView:self didSelectAtIndex:index title:self.indexTitleArr[index]];
    }
}

- (void)touchesMoved:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    
    NSInteger index = [self getIndexFromTouches:touches];
    
    if (currentIndex == index || index < 0 || index > self.indexTitleArr.count - 1) {
        return;
    }
    currentIndex = index;
    [self makeHock];

    if ([self.delegate respondsToSelector:@selector(indexBarView:didSelectAtIndex:title:)]) {
        [self.delegate indexBarView:self didSelectAtIndex:index title:self.indexTitleArr[index]];
    }
}

- (void)touchesEnded:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    
    if ([self.delegate respondsToSelector:@selector(indexBarViewDidEndTouch:)]) {
        
        [self.delegate indexBarViewDidEndTouch:self];
    }
}

- (void)touchesCancelled:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    
    if ([self.delegate respondsToSelector:@selector(indexBarViewDidEndTouch:)]) {
        
        [self.delegate indexBarViewDidEndTouch:self];
    }
}

///发出震动
- (void)makeHock {
    
    if (@available(iOS 10.0, *)) {
        if (isMoreThanIPhone7()) {
            UIImpactFeedbackGenerator *impactLight = [[UIImpactFeedbackGenerator alloc] initWithStyle:UIImpactFeedbackStyleLight];
            [impactLight impactOccurred];
            return;
        }
    }
    AudioServicesPlaySystemSound(1519);
}

///根据当前触摸手势获取index
- (NSInteger)getIndexFromTouches:(NSSet<UITouch *> *)touches {
    
    CGPoint touchPoint = [[touches anyObject] locationInView:self];
    return touchPoint.y / cellHeight;
}

@end
